package com.john.utils.common;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成二维码
 */
public class GenerateQR {

    public static void main(String[] args) throws Exception {
//         genCodeQR("http://wx.trhxyy.com/prod-api/signature/signature/forward?code=F80E75Y012689080E7531D6087BUkVQT1JUX19aXzc4OQ=="
//                 ,"C:\\Users\\Administrator\\Desktop\\test.png",600);

         try {
             /*String fullPath="C:\\Users\\Administrator\\Desktop\\111.png";
             String newPath="C:\\Users\\Administrator\\Desktop\\111.png";
             try {
                 ImageOptimizationUtil.binarization(fullPath,newPath);
             } catch (IOException e) {
                 e.printStackTrace();
             }*/
//             System.out.println(decodeQRCode("C:\\Users\\Administrator\\Desktop\\111.png"));
         }catch (Exception e){
             e.printStackTrace();
         }

        genCodeQR("http://wx.5205553.com/prod-api/signature/signature/forwardOauth?code=appointment","预约报备");

//        System.out.println(Base64Util.encode("3538611135"));
    }

    /**
     * 生成单个二维码图片
     *
     * @author lib
     * @date 20210909
     */
    public static String genCodeQR(String url,String title) {
        BufferedImage image = null;
        //这里可以先通过查库将需要生成的数据拼接到content中，然后作为二维码的标题
        try {
            image = QRCodeUtils.generateQrCodeBack(url, title);
            // 获取桌面路径，需要根据操作系统进行调整
            String desktopPath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator;

            // 指定文件保存的路径和文件名
            String imagePath = desktopPath + "image.png";

            // 保存图片到桌面
            try {
                ImageIO.write(image, "png", new File(imagePath));
                System.out.println("图片已保存到桌面。");
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("保存图片时发生错误。");
            }
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }


    /**
     * 识别二维码
     * @return
     * @throws IOException
     * @throws NotFoundException
     */
    public static String decodeQRCode(String path) throws IOException, NotFoundException {
        File qrCodeimage = new File(path);
        BufferedImage bufferedImage = ImageIO.read(qrCodeimage);
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result = new MultiFormatReader().decode(bitmap);
        return result.getText();
    }
 
/**
     * 生成二维码的方法
     * @param content 二维码内容
     * @param path 二维码图片保存路径
     * @param width 二维码宽度 正方形，高宽都一样
     */
    public static void genCodeQR(String content,String path, int width){
        // 设置二维码宽
        width = width > 0 ? width : 400;
        // 设置二维码高
        int height = width;
        // 设置二维码的后缀名称
        String format = "png";
        // 设置map集合要往二维码内添加的参数
        @SuppressWarnings("rawtypes")
        Map map = new HashMap();
        // 设置二维码的级别
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        // 设置二维码中文本的编码格式
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 设置二维码的外边框
        map.put(EncodeHintType.MARGIN, 5);
        try {
            // 创建生成二维码对象，调用方法将所需要的参数放入
            BitMatrix bm = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,map);
            // 创建path对象将物理地址放到file文件内，然后生成path对象
            Path paths = new File(path).toPath();
            // 使用writeToPath方法调用下载，里面的参数是下载的对象照片，下载的后缀名称，下载的物理路径地址
            MatrixToImageWriter.writeToPath(bm, format, paths);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
 
}